Localizing the CodeBehind in ASP.NET

In the previous chapters, we used localization only for the markup section of the webpage, but it is so easy to localize your string in the code bind. These are the techniques to do this, with some sample code to do this.

Direct access

Since The NET framework takes your global resource files and converts them into strongly typed sections, so you can actually refer to them in such context. For example, if you have a global resource file named MyGlobalResources.resx, with a resource line named HelloWorldString, you can access it like this:


lblHelloWorld.Text = Resources.MyGlobalResources.HelloWorldString;

GetLocalResourceObject()

With call to GetLocalResourceObject (), you can get a specific line from your local resource file. It has returned as an object, but you can always call the toasting () method on it. For this to work, to get a line from it, you must be inside the file that contains a local resource file, just specify the name of the line as the parameter of the method, such as:


lblHelloWorld.Text = GetLocalResourceObject("lblHelloWorld.Text").ToString();

GetGlobalResourceObject()

The GetGlobalResourceObject () method works much like the local version described above, but instead you will need to specify an additional parameter for the ASP.NET to be shown in the global resource file, which you want to see in the row The first parameter is the name of the resource class generated from the resource file, while the secondary parameter is the name of the row you are looking for To deny, in this way:


lblHelloWorld.Text = GetGlobalResourceObject("MyGlobalResources", "HelloWorldString").ToString();

The use of which approach depends mostly on the situation and what you like, but first of all there is a big advantage: Since it is very fast, the compiler will alert you that if you are trying to retrieve the line , He no longer exists any problems before going into production